﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MN.Framework.Common
{
    [Serializable]
    public abstract class ValidationRule
    {
        // Methods
        protected ValidationRule()
        {
        }

        public object GetValue(BaseEntity record, string propertyName)
        {
            object target = record;
            foreach (string str in propertyName.Split(new char[] { '.' }))
            {
                target = target.GetType().GetProperty(str).GetValue(target);
            }
            return target;
        }

        public abstract bool IsValid(BaseEntity record, string propertyName);
        public override string ToString()
        {
            if (string.IsNullOrEmpty(this.ErrorMessage))
            {
                return base.ToString();
            }
            return (base.ToString() + ",Error:" + this.ErrorMessage);
        }

        // Properties
        public string ErrorMessage { get; set; }
    }    

}
